package org.mypofolio.gui;

import java.awt.Component;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

public class FractionCellRenderer extends DefaultTableCellRenderer {
	
	  private SimpleDateFormat sd=new SimpleDateFormat("dd/MM/yyyy");
	
	  public FractionCellRenderer(int integer, int fraction, int align) {
		    this.integer = integer; // maximum integer digits
		    this.fraction = fraction; // exact number of fraction digits
		    this.align = align; // alignment (LEFT, CENTER, RIGHT)
		  }

		  protected void setValue(Object value) {
		    if (value != null && value instanceof Number) {
		      formatter.setMaximumIntegerDigits(integer);
		      formatter.setMaximumFractionDigits(fraction);
		      formatter.setMinimumFractionDigits(fraction);
		      setText(formatter.format(((Number) value).doubleValue()));
		    } else if (value != null && value instanceof Date) {
		      setText(sd.format(value));
		    }else{
		      super.setValue(value);
		    }
		    setHorizontalAlignment(align);
		  }

		  protected int integer;

		  protected int fraction;

		  protected int align;

		  protected static NumberFormat formatter = NumberFormat.getInstance();
}
